package com.example.a22310.myapplication.Activity;

import android.Manifest;
import android.app.FragmentManager;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.databinding.DataBindingUtil;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import com.example.a22310.myapplication.Beans.Train;
import com.example.a22310.myapplication.Dailog.ConsultDialog;
import com.example.a22310.myapplication.databinding.ActivityTrainDetailBinding;
import com.squareup.picasso.Picasso;


//训练详情
public class TrainDetail extends AppCompatActivity {
    private String clubName,phoneNumber = "18941132032";
    private final static int CALLPHONE_APPLY_KEY = 1;
    private Train train;
    private int club_id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityTrainDetailBinding binding = DataBindingUtil.setContentView(this,R.layout.activity_train_detail);
       // R.layout.activity_train_detail
        Intent intent=getIntent();
        train=(Train)intent.getSerializableExtra("train");
        clubName = train.getName();
        club_id = train.getClub_id();

        String str =  train.getTrain_picture().substring(52,train.getTrain_picture().length());
        Picasso.with(this).load(str).into(binding.trainPic);

        binding.setDetail(train);
        binding.trianRequsetion.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                consultOnlieDialog();
            }
        });
        binding.back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
        binding.trianApply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                intent.setClass(TrainDetail.this,ActivityApply.class);
                //    intent.putExtra("activityid",train.getId());
                bundle.putInt("activityid",train.getId());
                bundle.putString("name",train.getName());
                //   intent.putExtra("name",train.getName());
                //  intent.putExtra("type",train.getFlag()+"");
                bundle.putInt("type",train.getFlag());
                bundle.putString("price",train.getTrain_price());
                //intent.putExtra("price",train.getTrain_price());
                //  intent.putExtra("must",train.getTrain_must());
                bundle.putString("must",train.getTrain_must());
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

    }
    public void jumpToActivity(){
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        intent.setClass(this,ConsultActivity.class);
        bundle.putInt("club_id",club_id);
        bundle.putString("clubName",clubName);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    public void consultOnlieDialog() {
        FragmentManager fm = getFragmentManager();
        ConsultDialog dialog = new ConsultDialog();
        dialog.show(fm, "showPhotoSelDialog");
    }

    public void callPhone(){

        //1. 检查用户是否已经给我们授权了权限    相等则已经授权,不等则没授权
        if (ContextCompat.checkSelfPermission(TrainDetail.this, Manifest.permission.CALL_PHONE)
                != PackageManager.PERMISSION_GRANTED) {
            //2. 申请权限
            //参数:Context上下文,权限数组,申请码(申请码只要唯一就行)
            ActivityCompat.requestPermissions(TrainDetail.this,
                    new String[]{Manifest.permission.CALL_PHONE}, CALLPHONE_APPLY_KEY);
        } else {
            call();
        }

    }




    /**
     * 打电话
     */
    private void call() {
        try {
            Intent intent = new Intent(Intent.ACTION_CALL);
            intent.setData(Uri.parse("tel:"+phoneNumber));
            startActivity(intent);
        } catch (SecurityException e){   //这里要写SecurityException不然上面startActivity()会报红
            e.printStackTrace();
        }
    }

    //3. 判断是否申请成功 申请不管成功与否都会调用此方法
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {

        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode){
            case 1:
                //用户授权的结果会封装在grantResults中
                if(grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                    //申请成功
                    call();
                } else {

                    Toast.makeText(this, "You denied the permission", Toast.LENGTH_SHORT).show();
                }
                break;
            default:
        }
    }
}